package hos.demo.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.clickable
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import hos.demo.ui.theme.IntentTheme
import hos.intent.IntentResult
import hos.intent.IntentStart
import hos.permission.PermissionX

/**
 * <p>Title: Media </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @date : 2024-03-21 20:08
 * @version : 1.0
 */
class MediaActivity : AppCompatActivity() {

    private val permissionX = PermissionX.init(this)

    private var path by mutableStateOf("")
    private var outputUri: Uri? = null

    @OptIn(ExperimentalMaterial3Api::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            IntentTheme {
                LazyColumn(content = {
                    item {
                        ListItem(headlineText = {
                            Text(text = "拍照")
                        }, modifier = Modifier.clickable {
                            // 模拟器不行
                            permissionX.requestCamera {
                                // 拍照
                                IntentStart.createImage(this@MediaActivity, outputUri, 100)
                            }
                        })
                        ListItem(headlineText = {
                            Text(text = "录像")
                        }, modifier = Modifier.clickable {
                            // 模拟器不行
                            permissionX.requestCamera {
                                // 录像
                                IntentStart.createVideo(this@MediaActivity, outputUri, 100)
                            }
                        })
                        ListItem(headlineText = {
                            Text(text = "录音")
                        }, modifier = Modifier.clickable {
                            permissionX.requestCamera {
                                // 录音
                                IntentStart.createAudio(this@MediaActivity, outputUri, 100)
                            }
                        })
                        ListItem(headlineText = {
                            Text(text = "相册")
                        }, modifier = Modifier.clickable {
                            permissionX.requestAllStorage {
                                // 打开图片
                                IntentStart.choosePickImage(this@MediaActivity, 100)
                            }
                        })
                        ListItem(headlineText = {
                            Text(text = "路径：$path")
                        }, modifier = Modifier.clickable {
                            permissionX.requestAllStorage {
                                // 打开图片
                                IntentStart.openFile(this@MediaActivity, null, null, 200)
                            }
                        })
                    }
                })
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100) {
            val mediaUri = IntentResult.getMediaUri(resultCode, data, outputUri)

        }
    }
}